﻿/*****************************************************************************  

 Version: 0.1
 Website: http://www.visuagile.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/

#region Imports

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using VisualAgile.IBLL;
using VisualAgile.IDAL;
using VisualAgile.Model;

#endregion


namespace VisualAgile.BLL
{
    public class UserManager:IUserManager
    {
        #region Properties

        public IUserDao UserDao { get; set; }

        #endregion

        /// <summary>
        /// Create a user (back to contain the default value)
        /// </summary>
        /// <returns></returns>
        public User NewUser()
        {
            var user = new User();

            //System configuration can be used
            user.CreateTime = DateTime.Now;

            user.ValidFrom = DateTime.Now;
            user.ValidTo = Convert.ToDateTime("2999-12-31");
            user.UserName = "default";
            return user;
        }

        /// <summary>
        /// Save user
        /// </summary>
        /// <param name="user">User entity</param>
        /// <returns></returns>
        public User SaveUser(User user)
        {
            Validate(user);
            if (GetUser(user.UserID) != null)
                throw new  Exception("已经存在的用户");
            return  UserDao.Save(user);
        }

        /// <summary>
        /// Access to users
        /// </summary>
        /// <param name="userId">User ID</param>
        /// <returns></returns>
        public User GetUser(Guid userId)
        {
            return UserDao.FindById(userId);
        }

        /// <summary>
        /// Access to users
        /// </summary>
        /// <param name="userId">User ID</param>
        /// <returns></returns>
        public User GetByName(string userName)
        {
            return UserDao.FindByName(userName);
        }


        /// <summary>
        /// Users by project
        /// </summary>
        /// <param name="userId">Project ID</param>
        /// <returns></returns>
        public IList GetByProject(Guid projectId)
        {
            return UserDao.FindByProject(projectId);
        }


        /// <summary>
        /// Delete User
        /// </summary>
        /// <param name="userId">User ID</param>
        /// <returns></returns>
        public void DeleteUser(Guid userId)
        {
            var user = GetUser(userId);
            if (user == null) throw new Exception("NotFound");
            UserDao.Delete(user);
        }

        /// <summary>
        /// Update User
        /// </summary>
        /// <param name="user">User Entity</param>
        /// <returns></returns>
        public User  UpdateUser(User user)
        {
            return UserDao.SaveOrUpdate(user);
        }

        /// <summary>
        /// Access to users
        /// </summary>
        /// <returns></returns>
        public  IList GetAll()
        {
            return UserDao.FindAll();
        }

        private void Validate(User  user)
        {
            //TODO throw exception on error.
        }
    }
}
